<template>
  <div class="user">
    <div class="user-header">
      <h2>用户管理示例</h2>
      <p>展示复杂数据的状态管理和异步操作</p>
    </div>
    
    <div class="user-stats">
      <div class="stat-card">
        <div class="stat-number">{{ userStore.userCount }}</div>
        <div class="stat-label">总用户数</div>
      </div>
      <div class="stat-card">
        <div class="stat-number">{{ userStore.activeUserCount }}</div>
        <div class="stat-label">活跃用户</div>
      </div>
      <div class="stat-card">
        <div class="stat-number">{{ userStore.averageAge }}</div>
        <div class="stat-label">平均年龄</div>
      </div>
    </div>
    
    <div class="user-actions">
      <button @click="showAddForm = !showAddForm" class="btn btn-primary">
        {{ showAddForm ? '取消添加' : '添加用户' }}
      </button>
      <button @click="userStore.fetchUsers" :disabled="userStore.loading" class="btn btn-secondary">
        {{ userStore.loading ? '加载中...' : '刷新数据' }}
      </button>
    </div>
    
    <div v-if="showAddForm" class="add-user-form">
      <h3>添加新用户</h3>
      <form @submit.prevent="addUser">
        <div class="form-row">
          <input 
            v-model="newUser.name" 
            type="text" 
            placeholder="姓名" 
            required 
            class="form-input"
          >
          <input 
            v-model="newUser.email" 
            type="email" 
            placeholder="邮箱" 
            required 
            class="form-input"
          >
          <input 
            v-model.number="newUser.age" 
            type="number" 
            placeholder="年龄" 
            min="1" 
            max="120" 
            required 
            class="form-input"
          >
          <button type="submit" class="btn btn-success">添加用户</button>
        </div>
      </form>
    </div>
    
    <div class="user-filters">
      <button 
        @click="filter = 'all'" 
        :class="['filter-btn', { active: filter === 'all' }]"
      >
        全部 ({{ userStore.userCount }})
      </button>
      <button 
        @click="filter = 'active'" 
        :class="['filter-btn', { active: filter === 'active' }]"
      >
        活跃 ({{ userStore.activeUserCount }})
      </button>
      <button 
        @click="filter = 'inactive'" 
        :class="['filter-btn', { active: filter === 'inactive' }]"
      >
        非活跃 ({{ userStore.inactiveUsers.length }})
      </button>
    </div>
    
    <div class="user-list">
      <div 
        v-for="user in filteredUsers" 
        :key="user.id"
        class="user-card"
        :class="{ inactive: !user.active }"
      >
        <div class="user-info">
          <div class="user-name">{{ user.name }}</div>
          <div class="user-email">{{ user.email }}</div>
          <div class="user-age">年龄: {{ user.age }}</div>
          <div class="user-status" :class="{ active: user.active }">
            {{ user.active ? '活跃' : '非活跃' }}
          </div>
        </div>
        
        <div class="user-actions">
          <button 
            @click="userStore.toggleUserStatus(user.id)"
            :class="['btn', user.active ? 'btn-warning' : 'btn-success']"
          >
            {{ user.active ? '停用' : '启用' }}
          </button>
          <button 
            @click="editUser(user)"
            class="btn btn-secondary"
          >
            编辑
          </button>
          <button 
            @click="userStore.deleteUser(user.id)"
            class="btn btn-danger"
          >
            删除
          </button>
        </div>
      </div>
    </div>
    
    <div v-if="userStore.users.length === 0" class="empty-state">
      <div class="empty-icon">👥</div>
      <div class="empty-text">暂无用户数据</div>
    </div>
  </div>
</template>

<script>
import { ref, computed } from 'vue'
import { useUserStore } from '@/stores/user'

export default {
  name: 'User',
  setup() {
    const userStore = useUserStore()
    const showAddForm = ref(false)
    const filter = ref('all')
    
    const newUser = ref({
      name: '',
      email: '',
      age: null
    })
    
    const filteredUsers = computed(() => {
      switch (filter.value) {
        case 'active':
          return userStore.activeUsers
        case 'inactive':
          return userStore.inactiveUsers
        default:
          return userStore.users
      }
    })
    
    const addUser = () => {
      if (newUser.value.name && newUser.value.email && newUser.value.age) {
        userStore.addUser(newUser.value)
        newUser.value = { name: '', email: '', age: null }
        showAddForm.value = false
      }
    }
    
    const editUser = (user) => {
      const newName = prompt('请输入新姓名:', user.name)
      const newEmail = prompt('请输入新邮箱:', user.email)
      const newAge = prompt('请输入新年龄:', user.age)
      
      if (newName && newEmail && newAge) {
        userStore.updateUser(user.id, {
          name: newName,
          email: newEmail,
          age: parseInt(newAge)
        })
      }
    }
    
    return {
      userStore,
      showAddForm,
      filter,
      newUser,
      filteredUsers,
      addUser,
      editUser
    }
  }
}
</script>

<style scoped>
.user {
  max-width: 1000px;
  margin: 0 auto;
}

.user-header {
  text-align: center;
  margin-bottom: 2rem;
  background: rgba(255, 255, 255, 0.1);
  padding: 2rem;
  border-radius: 20px;
  backdrop-filter: blur(10px);
}

.user-header h2 {
  color: white;
  font-size: 2.5rem;
  margin-bottom: 1rem;
}

.user-header p {
  color: rgba(255, 255, 255, 0.8);
  font-size: 1.1rem;
}

.user-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1.5rem;
  margin-bottom: 2rem;
}

.stat-card {
  background: rgba(255, 255, 255, 0.95);
  padding: 1.5rem;
  border-radius: 15px;
  text-align: center;
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);
}

.stat-number {
  font-size: 2rem;
  font-weight: 700;
  color: #667eea;
  margin-bottom: 0.5rem;
}

.stat-label {
  color: #666;
  font-weight: 500;
}

.user-actions {
  display: flex;
  gap: 1rem;
  margin-bottom: 2rem;
  flex-wrap: wrap;
}

.btn {
  padding: 0.8rem 1.5rem;
  border: none;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-primary {
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: white;
}

.btn-secondary {
  background: #6c757d;
  color: white;
}

.btn-success {
  background: #28a745;
  color: white;
}

.btn-warning {
  background: #ffc107;
  color: #333;
}

.btn-danger {
  background: #dc3545;
  color: white;
}

.btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}

.add-user-form {
  background: rgba(255, 255, 255, 0.95);
  padding: 2rem;
  border-radius: 15px;
  margin-bottom: 2rem;
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);
}

.add-user-form h3 {
  color: #333;
  margin-bottom: 1.5rem;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr auto;
  gap: 1rem;
  align-items: end;
}

.form-input {
  padding: 0.8rem;
  border: 2px solid #e0e0e0;
  border-radius: 8px;
  font-size: 1rem;
  transition: border-color 0.3s ease;
}

.form-input:focus {
  outline: none;
  border-color: #667eea;
}

.user-filters {
  display: flex;
  gap: 1rem;
  margin-bottom: 2rem;
  flex-wrap: wrap;
}

.filter-btn {
  padding: 0.6rem 1.2rem;
  border: 2px solid #667eea;
  background: transparent;
  color: #667eea;
  border-radius: 25px;
  cursor: pointer;
  transition: all 0.3s ease;
  font-weight: 500;
}

.filter-btn.active,
.filter-btn:hover {
  background: #667eea;
  color: white;
}

.user-list {
  display: grid;
  gap: 1rem;
}

.user-card {
  background: rgba(255, 255, 255, 0.95);
  padding: 1.5rem;
  border-radius: 15px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.user-card.inactive {
  opacity: 0.7;
}

.user-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}

.user-info {
  flex: 1;
}

.user-name {
  font-size: 1.2rem;
  font-weight: 600;
  color: #333;
  margin-bottom: 0.5rem;
}

.user-email {
  color: #666;
  margin-bottom: 0.3rem;
}

.user-age {
  color: #666;
  margin-bottom: 0.3rem;
}

.user-status {
  display: inline-block;
  padding: 0.3rem 0.8rem;
  border-radius: 15px;
  font-size: 0.8rem;
  font-weight: 600;
  background: #dc3545;
  color: white;
}

.user-status.active {
  background: #28a745;
}

.user-actions {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.user-actions .btn {
  padding: 0.5rem 1rem;
  font-size: 0.9rem;
}

.empty-state {
  text-align: center;
  padding: 3rem;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 15px;
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);
}

.empty-icon {
  font-size: 4rem;
  margin-bottom: 1rem;
}

.empty-text {
  color: #666;
  font-size: 1.2rem;
}

@media (max-width: 768px) {
  .form-row {
    grid-template-columns: 1fr;
  }
  
  .user-card {
    flex-direction: column;
    gap: 1rem;
    align-items: stretch;
  }
  
  .user-actions {
    justify-content: center;
  }
  
  .user-filters {
    justify-content: center;
  }
}
</style>
